//
//  WXAnimationManager.m
//  ParentsCommunity
//
//  Created by tianlong on 2018/7/5.
//  Copyright © 2018年 XES. All rights reserved.
//

#import "WXAnimationManager.h"
#import "Masonry.h"

#define AniamtionManager [WXAnimationManager defaultManager]

@interface WXAnimationManager ()
@property (nonatomic, strong) WXAnimationView *aniView;
@property (nonatomic, strong) NSMutableArray *animates;
@end

@implementation WXAnimationManager

static WXAnimationManager *_instance;
+(instancetype)defaultManager {return [[self alloc]init];}
+(instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    });
    return _instance;
}
-(instancetype)copyWithZone:(NSZone *)zone {return _instance;}
-(instancetype)mutableCopyWithZone:(NSZone *)zone {return _instance;}

#pragma mark +动画相关
-(NSMutableArray *)animates {
    if (!_animates) {
        _animates = [NSMutableArray array];
    }
    return _animates;
}

+(void)playWithJson:(NSString *)json
              speed:(CGFloat)speed
        repeatCount:(NSInteger)repeatCount
         identifier:(NSString *)identifier
             layout:(void (^)(WXAnimationView *aniView))layout
           autoPlay:(BOOL)autoPlay
         completion:(void (^)(BOOL finished))completion; {
    // 先布局动画才能正常结束
    if (layout) {
        WXAnimationView *ani = [[WXAnimationView alloc] initWithJson:json speed:speed repeatCount:repeatCount completion:completion];
        ani.identifier = identifier;
        layout(ani);
        [AniamtionManager.animates addObject:ani];
        if (autoPlay) {
            [ani play];
        }
    }
}

+(void)playWithJson:(NSString *)json
              speed:(CGFloat)speed
        repeatCount:(NSInteger)repeatCount
         identifier:(NSString *)identifier
             inView:(UIView *)inView
           autoPlay:(BOOL)autoPlay
         completion:(void (^)(BOOL finished))completion {
    WXAnimationView *ani = [[WXAnimationView alloc] initWithJson:json speed:speed repeatCount:repeatCount completion:completion];
    ani.identifier = identifier;
    [inView addSubview:ani];
    CGFloat w       = [LiveSizeConfig getPPTWidth];
    CGFloat h       = [LiveSizeConfig getPPTHeight];
    CGFloat top     = [LiveSizeConfig getVerticalSpace];
    CGFloat left    = [LiveSizeConfig getHorizontalSpace];
    [ani mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width  .offset(w);
        make.height .offset(h);
        make.top    .offset(top);
        make.left   .offset(left);
    }];
    [AniamtionManager.animates addObject:ani];
    if (autoPlay) {
        [ani play];
    }
}

#pragma mark - 通过唯一标示获取标识对应的动画视图
+(WXAnimationView *)getAnimationByIdentifier:(NSString *)identifier {
    for (WXAnimationView *obj in AniamtionManager.animates) {
        if ([obj.identifier isEqualToString:identifier]) {
            return obj;
        }
    }
    return nil;
}

#pragma mark - 播放
+(void)playAllAnimations {
    for (WXAnimationView *obj in AniamtionManager.animates) {
        [obj play];
    }
}

+(void)playWithIdentifier:(NSString *)identifier {
    for (WXAnimationView *obj in AniamtionManager.animates) {
        if ([obj.identifier isEqualToString:identifier]) {
            [obj play];
            break;
        }
    }
}

#pragma mark - 暂停
+(void)pauseAllAnimations {
    for (WXAnimationView *obj in AniamtionManager.animates) {
        [obj pause];
    }
}

+(void)pauseWithIdentifier:(NSString *)identifier {
    for (WXAnimationView *obj in AniamtionManager.animates) {
        if ([obj.identifier isEqualToString:identifier]) {
            [obj pause];
            break;
        }
    }
}

#pragma mark - 停止
+(void)stopAniamtionWithIdentifier:(NSString *)identifier {
    NSMutableArray *dataM = [NSMutableArray arrayWithArray:AniamtionManager.animates];
    for (WXAnimationView *obj in dataM) {
        if ([obj.identifier isEqualToString:identifier]) {
            [obj stop];
            [obj removeFromSuperview];
            [dataM removeObject:obj];
            break;
        }
    }
    AniamtionManager.animates = [NSMutableArray arrayWithArray:dataM];
}

+(void)stopAllAnimations {
    NSMutableArray *dataM = [NSMutableArray arrayWithArray:AniamtionManager.animates];
    for (WXAnimationView *obj in dataM) {
        [obj stop];
        [obj removeFromSuperview];
    }
    AniamtionManager.animates = [NSMutableArray arrayWithArray:dataM];
    [AniamtionManager.animates removeAllObjects];
}

@end
